package com.leon.android.component.mine

import com.billy.cc.core.component.CC
import com.billy.cc.core.component.CCResult
import com.billy.cc.core.component.IComponent
import com.leon.android.common.action.IActionProcessor
import java.util.concurrent.atomic.AtomicBoolean

/**
 * @time:2019/12/16 10:25
 * @author:Leon
 * @description:
 */
class ComponentMine:IComponent {
    private val initialized: AtomicBoolean = AtomicBoolean(false)
    private val map: HashMap<String, IActionProcessor> = HashMap()

    private fun initProcessors() { //cc-regsiter插件会自动生成注册代码在此处，如：
        //add(new ShowLoginActivityProcessor())
    }

    private fun add(processor: IMineActionProcessor) {
        map[processor.actionName] = processor
    }

    override fun onCall(cc: CC): Boolean {
        if (initialized.compareAndSet(false, true)) {
            synchronized(map) { initProcessors() }
        }
        val actionName = cc.actionName
        val processor = map[actionName]
        if (processor != null) {
            return processor.onActionCall(cc)
        }
        CC.sendCCResult(cc.callId, CCResult.errorUnsupportedActionName())
        return false
    }

    override fun getName() = "mine"
}